/*
 * Copyright 2011 Dominik Pretzsch <dominik.pretzsch at gmail.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.blacksheep.jmoteextract.extraction;

import java.util.concurrent.Callable;
import de.blacksheep.jmoteextract.archive.Archive;
import de.blacksheep.jmoteextract.progress.ProgressUpdate;
import de.blacksheep.jmoteextract.progress.ProgressUpdateQueue;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * <code>ExtractionCallable</code> provides a ready-to-extract bundle <br>
 * consisting of the {@see Archive} and an optional <code>PipedWriter</code>. <br>
 * This can be added to the {@see ExtractionQueue} for automated <br>
 * extraction.
 * 
 * @author Dominik Pretzsch <dominik.pretzsch at gmail.com>
 * @see ArchiveExtractCallback
 */
public class ExtractionCallable implements Callable<Boolean> {
    
    /**
     * The archive.
     * 
     * @see Archive
     */
    private Archive archive;
    
    //private ProgressManager progressManager;
    private ProgressUpdateQueue progressUpdateQueue;
    
    private static final Logger LOGGER =  Logger.getLogger(ExtractionCallable.class.getName());

    public ExtractionCallable(Archive a) {
        this.archive = a;
        //this.progressManager = ProgressManager.getInstance();
        this.progressUpdateQueue = ProgressUpdateQueue.getInstance();
    }

    /**
     * This method is called by the thread pool it is added to.
     * 
     * @return true
     * @throws Exception 
     */
    public Boolean call() throws Exception {
        LOGGER.log(Level.FINE, "Starting extraction");
        
        //LOGGER.log(Level.FINE, "Setting ProgressManager to {0}", archiveHash);
        //archive.setProgressManager(progressManager, archiveHash);
        LOGGER.log(Level.FINE, "Try to open archive {0}", archive.getHash());
        archive.open();        
        LOGGER.log(Level.FINE, "Try to extract archive {0}", archive.getHash());
        archive.extract();
        LOGGER.log(Level.FINE, "Try to close archive {0}", archive.getHash());
        archive.close();
        
        // FIX: Lost update on small files
        LOGGER.log(Level.FINE, "Setting archive {0} to finished", archive.getHash());
        //progressManager.setFinished(archiveHash);
        progressUpdateQueue.enqueue(
                ProgressUpdate.TYPE_SET_FINISHED,
                new Object[]{
                    archive.getHash()
                });

        LOGGER.log(Level.FINE, "Extraction finished");        
        return true;
    }
}
